package com.ruoyi.kanaimqtt.common;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.ClientUtils;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.math.BigDecimal;

@Slf4j
@Component
public class WxSendMsgUtil {
    private static final String  MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";

    private static final String START_TEMPLATE_ID = "jVt_coDrStUCvcs4d62VXVyv8eL7BDAJSnvB1xeOxmM";
    private static final String END_TEMPLATE_ID = "WJjaqWyWX1VOoIfjGfVbvQTUZKIshFYcFSQoZ0YscXI";


    @Autowired
    com.ruoyi.kanaimqtt.common.wxTokenUtil wxTokenUtil;

    public void sendStartMsg(String openId, String startTime, String communityName,String deviceNum,String SocketNum, Long orderId){
        JSONObject msg = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        JSONObject thing1 = new JSONObject();
        JSONObject thing6 = new JSONObject();
        JSONObject thing7 = new JSONObject();
        JSONObject thing12 = new JSONObject();
        thing1.put("value",startTime);

        thing6.put("value",communityName);
        thing7.put("value",deviceNum);
        thing12.put("value",SocketNum);
        jsonObject.put("time2",thing1);
        jsonObject.put("thing5",thing6);
        jsonObject.put("character_string6",thing7);
        jsonObject.put("thing7",thing12);

        msg.put("touser",openId);
        msg.put("template_id",START_TEMPLATE_ID);
        msg.put("page","/pages/chargeDetail/index?chargeOrderId="+orderId);
        msg.put("data",jsonObject);
        String rest = null;
        try {
            rest = ClientUtils.doPostMethod(MSG_URL+ wxTokenUtil.getToken(),msg.toJSONString(),"UTF-8",null);
            log.info(rest);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendEndMsg(String openId, String endTime,Integer continueTime){
        JSONObject msg = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        JSONObject thing8 = new JSONObject();
        JSONObject amount9 = new JSONObject();
        JSONObject thing5 = new JSONObject();
        thing5.put("value","感谢使用！");

        thing8.put("value",endTime);

        amount9.put("value",getDate(continueTime));
        jsonObject.put("time3",thing8);
        jsonObject.put("thing4",amount9);
        jsonObject.put("thing5",thing5);
        log.info("充电时长[{}]",getDate(continueTime));


        msg.put("touser",openId);
        msg.put("template_id",END_TEMPLATE_ID);
        msg.put("page","pages/chargingRecord/index");
        msg.put("data",jsonObject);
        String rest = null;
        try {
            rest = ClientUtils.doPostMethod(MSG_URL+ wxTokenUtil.getToken(),msg.toJSONString(),"UTF-8",null);
            log.info(rest);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 转换秒为时分秒的形式
     *
     * @param seconds 待转换的秒
     * @return
     */
    public static String getDate(Integer seconds) {
        int h = seconds / 60 % 60;
        int m = seconds / 3600;
        return  m + "小时" +  h + "分" ;
    }

}
